/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * alertEliminar.java
 *
 * Created on 04-may-2009, 9:57:39
 */

package alertas;

import gestion.Trabajo;
import clientes.Doctor;
import productos.Producto;
import clientes.Cliente;
import laboratorio.*;
import com.mysql.jdbc.jdbc2.optional.MysqlXAConnection;
import gestion.Fecha;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.*;
import javax.persistence.Query;

/**
 *
 * @author Juan Antonio Tirado
 */
public class alertEliminar extends javax.swing.JFrame {
    //ATRIBUTOS
    private ListIterator it;
    private Productos VentanaProductoOld;
    private Clientes VentanaClienteOld;
    private Doctores VentanaDoctorOld;
    private Trabajos VentanaTrabajoOld;
    private String tipo;
    private Query q1;

    //private Producto producto;
    /** Creates new form alertEliminar */
    public alertEliminar() {
        initComponents();
    }
    public alertEliminar(Productos productosOld) {
        initComponents();
        VentanaProductoOld=productosOld;
        tipo="productos";
    }
    public alertEliminar(Clientes clientesOld) {
        initComponents();
        VentanaClienteOld=clientesOld;
        tipo="clientes";
    }
    public alertEliminar(Doctores doctoresOld) {
        initComponents();
        VentanaDoctorOld=doctoresOld;
        tipo="doctores";
    }
    public alertEliminar(Trabajos trabajosOld) {
        initComponents();
        VentanaTrabajoOld=trabajosOld;
        tipo="trabajos";
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lpdPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("lpdPU").createEntityManager();
        productoQuery = java.beans.Beans.isDesignTime() ? null : lpdPUEntityManager.createQuery("SELECT p FROM Producto p");
        productoList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : productoQuery.getResultList();
        doctorQuery = java.beans.Beans.isDesignTime() ? null : lpdPUEntityManager.createQuery("SELECT d FROM Doctor d");
        doctorList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : doctorQuery.getResultList();
        clienteQuery = java.beans.Beans.isDesignTime() ? null : lpdPUEntityManager.createQuery("SELECT c FROM Cliente c");
        clienteList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : clienteQuery.getResultList();
        trabajoQuery = java.beans.Beans.isDesignTime() ? null : lpdPUEntityManager.createQuery("SELECT t FROM Trabajo t");
        trabajoList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : trabajoQuery.getResultList();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(laboratorio.LaboratorioApp.class).getContext().getResourceMap(alertEliminar.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                salir(evt);
            }
        });

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                aceptarEliminar(evt);
            }
        });

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setIcon(resourceMap.getIcon("jLabel1.icon")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(149, 149, 149)
                        .addComponent(jButton2)
                        .addGap(36, 36, 36)
                        .addComponent(jButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 311, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(34, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(25, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(58, 58, 58)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton1))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void salir(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_salir
        this.setVisible(false);
    }//GEN-LAST:event_salir

    private void aceptarEliminar(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_aceptarEliminar
        
        if(tipo.equals("productos")){
            Producto p2=VentanaProductoOld.getProducto();
            Producto p=productoList.get(productoList.indexOf(p2));
            lpdPUEntityManager.getTransaction().begin();
            lpdPUEntityManager.remove(p);
            lpdPUEntityManager.getTransaction().commit();
            this.setVisible(false);
            Producto pAux=productoList.get(productoList.indexOf(p2)-1);
            VentanaProductoOld.setProductoSelec(pAux);

        }else if(tipo.equals("doctores")){
            Doctor d2=VentanaDoctorOld.getDoctor();
            Doctor d=doctorList.get(doctorList.indexOf(d2));
            lpdPUEntityManager.getTransaction().begin();
            lpdPUEntityManager.remove(d);
            lpdPUEntityManager.getTransaction().commit();
            this.setVisible(false);
            Doctor dAux=doctorList.get(doctorList.indexOf(d2)-1);
            VentanaDoctorOld.setDoctorSelec(dAux);

        }else if(tipo.equals("clientes")){
            Cliente c2=VentanaClienteOld.getCliente();
            Cliente c=clienteList.get(clienteList.indexOf(c2));
            lpdPUEntityManager.getTransaction().begin();
            lpdPUEntityManager.remove(c);
            lpdPUEntityManager.getTransaction().commit();
            this.setVisible(false);
            Cliente cAux=clienteList.get(clienteList.indexOf(c2)-1);
            VentanaClienteOld.setClienteSelec(cAux);

        }else if(tipo.equals("trabajos")){
            Trabajo t2=VentanaTrabajoOld.getTrabajo();
            Trabajo t=trabajoList.get(trabajoList.indexOf(t2));
            Query q=lpdPUEntityManager.createQuery("SELECT a FROM Albaran a WHERE a.iDtrabajo="+t2.getIDtrabajo());
            List albaranes=q.getResultList();
            if(albaranes.isEmpty()){
                q=lpdPUEntityManager.createQuery("SELECT f FROM Fecha f WHERE f.fechaPK.iDtrabajo="+t2.getIDtrabajo());
                List fechas=q.getResultList();
                Iterator it2 =fechas.iterator();
                while (it2.hasNext()){
                    Fecha fecha =(Fecha)it2.next();
                    lpdPUEntityManager.getTransaction().begin();
                    lpdPUEntityManager.remove(fecha);
                    lpdPUEntityManager.getTransaction().commit();
                }
                lpdPUEntityManager.getTransaction().begin();
                lpdPUEntityManager.remove(t);
                lpdPUEntityManager.getTransaction().commit();
                Trabajo tAux=trabajoList.get((trabajoList.indexOf(t2))-2);
                VentanaTrabajoOld.setTrabajoSelec(tAux);

            }
            
            
            
            this.setVisible(false);

            
        }
    }//GEN-LAST:event_aceptarEliminar

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new alertEliminar().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.util.List<clientes.Cliente> clienteList;
    private javax.persistence.Query clienteQuery;
    private java.util.List<clientes.Doctor> doctorList;
    private javax.persistence.Query doctorQuery;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.persistence.EntityManager lpdPUEntityManager;
    private java.util.List<productos.Producto> productoList;
    private javax.persistence.Query productoQuery;
    private java.util.List<gestion.Trabajo> trabajoList;
    private javax.persistence.Query trabajoQuery;
    // End of variables declaration//GEN-END:variables

}
